﻿(function (root) {
    if (!root.Ktot) {
        root.Ktot = {};
    }

    root.Ktot.BaseController = function (self) {
        self = self || this;
        self.validationErrors = [];
        self.addValidationError = function (propName, errorMessage) {            
            var error = {
                propertyName: propName,
                errorMessage: errorMessage
            };

            self.validationErrors.push(error);
        };

        self.addRequiredError = function (propName, displayName) {
            self.addValidationError(propName, 'The field ' + displayName + ' is required.');
        };

        self.propertyHasError = function (propName) {
            return Enumerable.From(self.validationErrors)
                .Any(function (e) { return e.propertyName == propName; });
        };

        self.getErrorForProperty = function (propName) {
            var error = Enumerable.From(self.validationErrors)
                .Where(function (e) { return e.propertyName == propName; })
                .FirstOrDefault();

            if (!error) {
                return null;
            }

            return error.errorMessage;
        };

        self.isInvalid = function () {
            return self.validationErrors.length > 0;
        };

        self.clearValidationErrors = function () {
            self.validationErrors = [];
        };

        self.onValidate = function () {

        };

        self.validate = function () {
            self.clearValidationErrors();
            self.onValidate();
            return !self.isInvalid();
        };

        self.onSave = function () {

        };

        self.showValidationErrors = function () {
            var errors = Enumerable.From(self.validationErrors).Select("$.errorMessage").ToArray();
            Ktot.UI.alert(Utils.String.join(errors, '<br>'));
        };

        self.save = function () {
            if (!self.validate()) {
                self.showValidationErrors();
                return;
            }

            self.onSave();
        };

        self.defaultErrorHandler = function (errors) {
            self.clearValidationErrors();
            if (errors) {
                if (!angular.isArray(errors)) {
                    errors = [errors];
                }                
                    errors.forEach(function (err) {
                        if (angular.isString(err)) {
                            self.validationErrors.push({ property: 'model', errorMessage: err });
                        }
                        else {
                            self.validationErrors.push(err);
                        }
                    });                
            }

            if (self.isInvalid()) {
                self.showValidationErrors();
            }
        };

        self.clearValidationErrorsForProperty = function (propName) {
            var errors = Enumerable.From(self.validationErrors)
                .Where(function (p) { return p.propertyName == propName; }).ToArray();
            if (errors.length > 0) {
                self.validationErrors = Enumerable.From(self.validationErrors).Except(errors).ToArray();
            }
        };
    };
})(window);